package com.example.cofeed.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ObjectUtils;
import  com.example.cofeed.util.ToastUtils;;
import com.example.cofeed.R;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.IntentKeyConstant;
import com.lzy.okgo.model.Response;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BindPhoneActivity extends BaseActivity {

    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.tv_get_code)
    TextView mTvGetCode;
    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.confirm_tv)
    TextView mConfirmTv;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_phone;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setMainTitle("绑定手机");

    }

    @OnClick({R.id.tv_get_code, R.id.confirm_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                getCode();
                break;
            case R.id.confirm_tv:
                String phone = mEtPhone.getText().toString().trim();
                String code = mEtCode.getText().toString().trim();
                if (ObjectUtils.isEmpty(phone)){
                    ToastUtils.showShort("请输入手机号码");
                    return;
                }
                if (ObjectUtils.isEmpty(code)){
                    ToastUtils.showShort("请输入手机验证码");
                    return;
                }

                ApiService.password_bind_mobile(this, phone, code, new DialogCallback<LzyResponse<Object>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<Object>> response) {
//                        showToast(response.body().message);
//                        finish();

                        ApiService.password_mobile_status(this, phone, new DialogCallback<LzyResponse<Object>>() {
                            @Override
                            public void onSuccess(Response<LzyResponse<Object>> response) {
                                Map<String, Double> map = (Map<String, Double>)response.body().data;
                                Double i = map.get("accountid");
                                int accountid = (new Double(i)).intValue();

                                if (i > 0){
                                    Intent intent = new Intent(mContext, ForgetPwdActivity.class);
                                    intent.putExtra(IntentKeyConstant.PHONE, phone);
                                    intent.putExtra(IntentKeyConstant.ACCOUNT_ID, accountid);
                                    startActivity(intent);
                                    finish();

                                }else {
                                    ToastUtils.showShort("您的手机没有绑定账号");
                                }
                            }
                        });
                    }
                });

                break;
        }
    }

    private void getCode() {
        String phone = mEtPhone.getText().toString().trim();
        if (ObjectUtils.isEmpty(phone)){
            ToastUtils.showShort("请输入手机号码");
            return;
        }
        ApiService.verifycode(this, phone, 2, new DialogCallback<LzyResponse<Object>>(this) {
            @Override
            public void onSuccess(Response<LzyResponse<Object>> response) {
                mTvGetCode.setClickable(false);
                mTvGetCode.setText(expired_time + "秒");
                mHandler.postDelayed(runnable, 1000);

            }
        });
    }

    private Handler mHandler = new Handler();
    private int expired_time = 60;

    /**
     * 60秒倒计时
     */
    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            expired_time--;
            mTvGetCode.setText(expired_time + "秒");
            if (expired_time == 0) {
                initVcode();
                return;
            }
            mHandler.postDelayed(this,1000);
        }
    };

    /*
     * 60秒过后初始化各状态
     */
    private void initVcode(){
        expired_time = 60;
        mTvGetCode.setClickable(true);
        mTvGetCode.setText("获取验证码");
    }
}
